/**
 * 
 */
package com.dcivision.framework.taglib;

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTag;
import javax.servlet.jsp.tagext.Tag;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.util.ResponseUtils;

import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.taglib.html.ImgTag;
import com.dcivision.framework.taglib.html.OptionsTag;
import com.dcivision.framework.taglib.html.SelectTag;
import com.dcivision.framework.taglib.html.StandardButtonTag;

/**
 * @author lun.au
 *
 */
public class TagUtil {

  public static final String REVISION = "$Revision: 1.1.2.2 $";
  
  protected static Log log = LogFactory.getLog(TagUtil.class.getName());
  
  public static String renderSelectTag(PageContext pageContext, SelectTag selectTag, OptionsTag optionsTag) throws JspException {
    BodyContent bodyContent = pageContext.pushBody();
    JspWriter out = pageContext.getOut();
    Writer content = new StringWriter();
    ResponseUtils.write(pageContext, "<!-- Start select element -->");
    int evalSelect = selectTag.doStartTag();
    if (evalSelect != Tag.SKIP_BODY) {
      if (evalSelect != Tag.EVAL_BODY_INCLUDE) {
        BodyContent _bc = pageContext.pushBody();
        out = _bc;
        selectTag.setPageContext(pageContext);
        selectTag.setBodyContent(_bc);
        selectTag.doInitBody();
      }
      do{
        // out.write(...) here if needed
        try {
          optionsTag.setPageContext(pageContext);
          String optionsHTML = renderTag (pageContext, optionsTag);
          out.write(optionsHTML);
log.debug(optionsHTML);          
        }catch(IOException ioe){
          log.error(ioe.getMessage(), ioe);
        }
        int evalSelectDoAfterBody = selectTag.doAfterBody();
        if (evalSelectDoAfterBody != BodyTag.EVAL_BODY_AGAIN) {
          break;
        }
      }while(true);
      if (evalSelect != Tag.EVAL_BODY_INCLUDE) {
        out = pageContext.popBody();
      }
    }
    selectTag.doEndTag();
    ResponseUtils.write(pageContext, "<!-- End select element -->");
    pageContext.popBody();
    selectTag.release();
    writeBodyContent(bodyContent, content);
    return content.toString();
  }
  
  public static String renderTag(PageContext pageContext, Tag tag) throws JspException {
    BodyContent bodyContent = pageContext.pushBody();
    Writer content = new StringWriter();
    tag.setPageContext(pageContext);
    tag.doStartTag();
    tag.doEndTag();
    pageContext.popBody();
    tag.release();
    writeBodyContent(bodyContent, content);
    return content.toString();
  }
  
  /**
   * get the imgButton 
   * tc.tang 
   * @param label
   * @param onclick
   * @param href
   * @return
   * @throws JspException
   * @throws IOException
   */
  public static String renderStandardButton(PageContext pageContext, String label, String onclick, String link) throws JspException {
    StandardButtonTag button = new StandardButtonTag();
    button.setPageContext(pageContext);
    button.setLabel(label);
    button.setOnClick(onclick);
    button.setLink(link);
    button.setCommon(true);
    return renderTag(pageContext, button);
  }
  
  public static ImgTag getImageTag(String src) {
    ImgTag imgTag = new ImgTag();
    imgTag.setSrc(src);
    imgTag.setCommon(true);
    return imgTag;
  }
  
  public static String renderImage(PageContext pageContext, String src, boolean common) throws JspException {
    ImgTag imgTag = getImageTag(src);
    imgTag.setPageContext(pageContext);
    if(!common){
      String sessionLocale = ((SessionContainer)pageContext.getSession().getAttribute(GlobalConstant.SESSION_CONTAINER_KEY)).getSessionLocale().toString();
      imgTag.setCommon(false);
      imgTag.setLocale(sessionLocale);
    }
    return renderTag(pageContext, imgTag);
  }
  
  public static void writeBodyContent(BodyContent bodyContent, Writer writer) {
    try {
      bodyContent.writeOut(writer);
    }catch(IOException ioe){
      log.error(ioe.getMessage(), ioe);
    }
  }
}